package cn.com.shaom.learning.sb.session.example3;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.session.security.web.authentication.SpringSessionRememberMeServices;

/**
 * ClassName: WebSecurityConfig
 * Description:
 * Date: 2018/1/10 19:50 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    /*@Bean
    @Override
    public UserDetailsService userDetailsService() {
        UserDetails build = User.withUsername("user").password("password").roles("USER").build();
        return new InMemoryUserDetailsManager(build);
    }*/

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("admin").password("admin").roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/resources/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .httpBasic()//<1>
//                .and().rememberMe().rememberMeServices(rememberMeServices())
                .and()
                .logout().permitAll();
    }

    @Bean
    public RememberMeServices rememberMeServices() {
        SpringSessionRememberMeServices rememberMeServices =
                new SpringSessionRememberMeServices();
        // optionally customize
        rememberMeServices.setAlwaysRemember(true);
        rememberMeServices.setValiditySeconds(10000);
        return rememberMeServices;
    }
}